package com.peng.community.util;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.DigestUtils;

import java.util.Map;
import java.util.UUID;

public class CommunityUtils {

    public static String getUUID() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    public static String md5(String key) {
        if (StringUtils.isAllBlank(key)) {
            return null;
        }
        return DigestUtils.md5DigestAsHex(key.getBytes());
    }


    public static String toJsonString(int code, String msg, Map<String, Object> map) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",code);
        jsonObject.put("msg",msg);
        if (map != null) {
            for (String s : map.keySet()) {
                jsonObject.put(s,map.get(s));
            }
        }
        return jsonObject.toJSONString();
    }

    public static String toJsonString(int code, String msg) {
        return toJsonString(code,msg,null);
    }

    public static String toJsonString(int code) {
        return toJsonString(code,null);
    }
}
